using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.SessionState;

namespace SFS.SampleApp.BR.Tests
{
    public class CustomTestContext
    {

        public bool AddFKRelationsIfNotExist {
            get {
                bool result = true;
                if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["AddRelationsIfNotExist"])) {
                    bool.TryParse(ConfigurationManager.AppSettings["AddRelationsIfNotExist"], out result);
                }
                return result;
            }
        }
        public bool AddChildRelationsIfNotExist
        {
            get
            {
                bool result = true;
                if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["AddRelationsIfNotExist"]))
                {
                    bool.TryParse(ConfigurationManager.AppSettings["AddRelationsIfNotExist"], out result);
                }
                return result;
            }
        }

        public bool AddFKRelationsInSameTransactionIfNotExist
        {
            get
            {
                bool result = true;
                if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["AddRelationsInSameTransactionIfNotExist"]))
                {
                    bool.TryParse(ConfigurationManager.AppSettings["AddRelationsInSameTransactionIfNotExist"], out result);
                }
                return result;
            }
        }
        public string GetNewString(int? maxChars)
        {
            if (maxChars != null )
                return UtilityTest.RandomText(maxChars.Value);
            else
                return UtilityTest.RandomText(15);

        }
        public Int32 GetNewInt32()
        {
            return GetSmallInt(10);
        }

        public Int16 GetNewInt16()
        {
            return Int16.Parse(UtilityTest.RandomInt(32000).ToString());
        }

        public Guid GetNewGuid()
        {
            return Guid.NewGuid();
        }


        public string GetNewString()
        {
            return GetNewString(null);

        }
        public string UniqueString
        {
            get
            {
                return uniquetext;
            }
        }
        public int UniqueInt32
        {
            get
            {
                return uniqueint;
            }
        }
        public int UniqueInt16
        {
            get
            {
                return uniqueint;
            }
        }
        public List<object> DefaultPropertyValues { get; set; }

        public Guid UniqueGuid
        {
            get
            {
                return uniqueguid;
            }
        }
        public DateTime UniqueDateTime
        {
            get
            {
                return DateTime.Now;
            }
        }
        public CustomTestContext()
        {
            _currentTime = DateTime.Now;
            uniqueint = new Random().Next();
            uniquetext = "" + GetNewString(10);
            uniqueguid = Guid.NewGuid();
            InsertChildMany = 3;
            CreateMany = 3;
            DeleteMany = 3;
            DefaultPropertyValues = new List<object>();

        }
        public int CreateMany { get; set; }
        public int DeleteMany { get; set; }

        public string NewTextTest()
        {


            return UtilityTest.RandomText(30);

        }
        public string NewTextTest(int max)
        {


            return UtilityTest.RandomText(max);

        }

        public Int32 NewInt32Test
        {
            get
            {

                return UtilityTest.RandomInt();
            }
        }
        public Int16 NewInt16Test
        {
            get
            {
                

                return Int16.Parse(UtilityTest.RandomInt(32000).ToString());
            }
        }
        public Boolean NewBooleanTest
        {
            get
            {

                return UtilityTest.RandomBool();
            }
        }
        public string NewStringTest
        {
            get
            {

                return UtilityTest.RandomText(30);
            }
        }
        public DateTime NewDateTimeTest
        {
            get
            {

                return UtilityTest.RandomDate();
            }
        }

        public decimal NewDecimalTest
        {
            get
            {

                return UtilityTest.RandomDecimal();
            }
        }
        public int GetSmallInt(int max)
        {
            return new Random().Next(10);
        }
        public int NewIntegerTest
        {
            get
            {

                int n = new Random().Next();
                return n;
            }
        }
        public bool RecordsCounted { get; set; }
        public int Records { get; set; }
        public List<CustomTestContext> Contexts { get; set; }
        int lasttext = 0;
        public int uniqueint { get; set; }
        public string uniquetext { get; set; }
        public Guid uniqueguid { get; set; }
        private DateTime? _currentTime = null;
        public int InsertChildMany { get; set; }
        public string uniqueauxtime
        {
            get
            {
                return _currentTime.Value.ToString("yyMMdd-hhmm");
            }
        }
    }
    public class UtilityTest
    {
      
        private string Text = "";
        public static int RandomInt(){
            return RandomInt(900000);
              
        }
        public static decimal RandomDecimal(){
                Random random = new Random();
                return Decimal.Parse( random.Next(1, 500).ToString() + "." + random.Next(0,99).ToString());
        }
        public static DateTime  RandomDate()
        {
            Random random = new Random();
            return DateTime.Now.AddDays(- random.Next(1,300));
        }
        public static Boolean RandomBool()
        {
            Random random = new Random();
            int result = random.Next(0, 1);
            if (result == 1)
                return true;
            else
                return false;

        }
        public static string RandomText(int lenght) {
            string allText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ut augue non mi venenatis tristique sit amet at urna. Nam tincidunt magna sed dui hendrerit feugiat. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur nisl libero, pulvinar nec dignissim eu, egestas sed velit. Duis felis augue, elementum ut tincidunt et, vehicula vitae ipsum. Nam quis lobortis urna. Nunc est turpis, lacinia eu pulvinar ut, eleifend ac nulla. Duis ut ipsum eget justo fermentum lacinia vitae sed diam. Vestibulum congue viverra magna in tincidunt. Donec turpis magna, viverra at convallis eget, feugiat id quam. Donec a mi orci, ut tristique metus. Vivamus in laoreet augue. Praesent molestie arcu vitae tortor imperdiet posuere. Duis purus neque, sagittis eget sagittis quis, posuere sit amet mi. Morbi aliquam laoreet auctor. Fusce sed augue ut elit consequat tempus. Sed sit amet sapien eget magna rhoncus viverra vitae at lorem. Cras velit libero, hendrerit eget semper sit amet, bibendum ac turpis. Maecenas libero tortor, congue a viverra in, consequat id odio. Morbi in rutrum quam. Ut et interdum tellus. Donec vel eros est. Fusce vel vehicula felis. Vivamus a scelerisque eros. Sed felis enim, eleifend ut ultrices sit amet, iaculis id dui. Vivamus interdum fermentum orci sit amet vehicula. Quisque vitae neque nec augue fringilla lacinia et eu libero. Vestibulum iaculis velit sed quam porttitor sed iaculis nibh venenatis. Vivamus sit amet mauris at massa condimentum cursus vitae non nunc.";
            
            int max = allText.Length;
           
            int start = RandomInt(100);
            if (lenght > 0)
                max = lenght;
            else 
                max = max - start;
            max = RandomInt(max);
            
            return allText.Substring(start, max);
        }


        internal static int RandomInt(int max)
        {
            Random random = new Random(DateTime.Now.Millisecond);
            return random.Next(max);
        }
    }
}
